﻿CREATE TRIGGER [TRG_CancelPendingOrderOnFail]
    ON [dbo].[tblTransaction]
    FOR UPDATE 
    AS 
    BEGIN
    	SET NOCOUNT ON

		update oh set OrderStatusCode = 'C',
			CanceledReasonID = 2
		from tblOrderHeader oh
			join inserted on oh.OrderID = inserted.OrderID
			join deleted on inserted.TransactionID = deleted.TransactionID
		where inserted.TransactionStatusCode = 'F'
			and deleted.TransactionStatusCode <> 'F'
			and oh.OrderStatusCode = 'P'
    END